﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.ComponentModel;
using System.Windows.Controls.Primitives;

namespace Gmantis.Controls
{
    public static class PlatformIndependent
    {
        // Fields
        public static double DPI = 1.3333333333333333;

        // Methods
        internal static ControlTemplate AdjustContentControlTemplate(ControlTemplate template)
        {
            return template;
        }

        public static void BeginInvoke(this FrameworkElement elem, Action action)
        {
            elem.Dispatcher.BeginInvoke(action);
        }

        public static FrameworkElement GetLogicalFocusedElement(this FrameworkElement relativeToElement)
        {
            if (!Application.Current.IsRunningOutOfBrowser)
            {
                return (FocusManager.GetFocusedElement() as FrameworkElement);
            }
            if (relativeToElement == null)
            {
                return null;
            }
            Window element = Window.GetWindow(relativeToElement);
            if (element == null)
            {
                return null;
            }
            return (FocusManager.GetFocusedElement(element) as FrameworkElement);
        }

        public static int GetPlatformKeyCode(this System.Windows.Input.KeyEventArgs e)
        {
            return e.PlatformKeyCode;
        }

        public static UIElement GetRootVisual(this UIElement element)
        {
            return Application.Current.RootVisual;
        }

        public static T LoadXaml<T>(string strXaml) where T : class
        {
            return (T)XamlReader.Load(strXaml);
        }

        public static FrameworkElement GetKeyboardFocusedElement()
        {
            return (FocusManager.GetFocusedElement() as FrameworkElement);
        }

        public static bool IsInDesignMode(DependencyObject obj)
        {
            return DesignerProperties.GetIsInDesignMode(obj);
        }

        internal static bool IsInVisualTree(this FrameworkElement elem)
        {
            FrameworkElement reference = elem;
            FrameworkElement element2 = null;
        Label_0008: ;
            element2 = (reference.Parent ?? VisualTreeHelper.GetParent(reference)) as FrameworkElement;
            if (element2 != null)
            {
                reference = element2;
                goto Label_0008;
            }
            Window window = null;
            if (Application.Current.IsRunningOutOfBrowser)
            {
                window = Window.GetWindow(elem);
            }
            return (((reference == Application.Current.RootVisual) || ((reference is Popup) && ((Popup)reference).IsOpen)) || ((window != null) && window.IsActive));
        }
    }
}
